package com.gouldzd.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.gouldzd.dao.MemberDao;
import com.gouldzd.pojo.Member;
import com.gouldzd.service.MemberService;
import com.gouldzd.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author zhangdan
 * @Description //TODO 会员服务
 * @Date 9:58 2020/5/7
 * @Param
 * @return
 **/
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //判断当前用户是否为会员（查询会员表来确定）
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //保存会员信息
    @Override
    public void add(Member member) {
        String password = member.getPassword();

        //判断用户是否输入密码(是否非快速登录注册)
        if(password != null){
            //非快速登录注册
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }

        //保存用户
        memberDao.add(member);
    }

    //根据月份查询会员数量
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }
}
